﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kitware.VTK;

namespace HAVTKPack
{


     public   class ScalarBarFilter:GeometryFilter 
     {

         #region 构造函数

         public ScalarBarFilter(string id)
             : base(id)
         { 

         
         
         }

         #endregion

         #region 字段属性


         #endregion


         #region VTK流水线所需要的对象

         //vtkUnstructuredGrid _unStructuredGridData;

         //vtkConnectivityFilter _con;
         //vtkGeometryFilter _geo;
         //vtkPolyData _polyData;


         //vtkScalarBarActor scalarBarActor;
         vtkLookupTable lut;
         //vtkDataSetMapper mapper;
         //vtkActor actor;
         vtkScalarBarWidget scalarWidget;

         #endregion

         public override void Render()
         {
             #region 数据的基本操作

             //_unStructuredGridData = (vtkUnstructuredGrid)this.ParentNode.Geometry.OutPutData;

             //if (this._unStructuredGridData == null)
             //{
             //    return;
             //}
             //_con = vtkConnectivityFilter.New();
             //_con.SetInput(_unStructuredGridData);

             //_geo = vtkGeometryFilter.New();
             //_geo.SetInput(_con.GetOutput());

             //_polyData = vtkPolyData.New();
             //_polyData = _geo.GetOutput();


             //this.OutputPolyData = _geo.GetOutput();

   

             #endregion


             #region 创建查询表

             //vtkColorTransferFunction colorTransFunction = vtkColorTransferFunction.New();
            



             //创建查询表  注意和其他地方的查询表的参数的一致性

             //某些参数可以通过外部设置

             lut = vtkLookupTable.New();

             //设置色调范围
             lut.SetHueRange(0,1);
             //设置饱和度范围
             lut.SetSaturationRange(1, 1);
             lut.SetValueRange(1, 1);
             lut.SetAlpha(1);

             lut.SetNumberOfColors(256);
             //关于SetNumberOfColors的说明：Set the number of colors in the lookup table.
             //Use SetNumberOfTableValues() instead, it can be used both before and after the table has been built whereas SetNumberOfColors() has no effect after the table has been built. 



             lut.SetTableRange(0,1000);//指定scalar值的范围
             lut.SetNumberOfTableValues (10);
             //lut.SetNumberOfTableValues(1000);
          
             //for (int k = 0; k < 1000; k++)
             //{
             //    lut.SetTableValue(k, k / 1000.0, 1, 1 - k / 1000.0, Math.Abs(k -500) / 1000.0);
             //    //lut.SetTableValue(k, k / 1000.0, k / 1000.0, k / 1000.0, 1);
             //    //lut.SetTableValue(k, (1000 - k) / 1000.0, (1000 - k) / 1000.0, (1000 - k) / 1000.0, 1);
             //}

             lut.Build();


             //mapper = vtkDataSetMapper.New();
             //mapper.SetLookupTable(lut);
             //mapper.SetInput(_unStructuredGridData);


             //actor = vtkActor.New();
             //actor.SetMapper(mapper);
             //actor.VisibilityOn();


             #endregion


          

             #region  通过vtkScalarBarActor表现ScalarBar


             //scalarBarActor = vtkScalarBarActor.New(); 
             //scalarBarActor.SetLookupTable(lut);
             //scalarBarActor.SetTitle("Head");
             //scalarBarActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport();
             //scalarBarActor.GetPositionCoordinate().SetValue(0.1, 0.01);
             //scalarBarActor.SetOrientationToHorizontal();//查询表的方向  垂直还是横向等
             ////scalarBar.SetOrientationToVertical();
             //scalarBarActor.SetWidth(0.4);
             //scalarBarActor.SetHeight(0.08);

             #endregion


             #region 通过vtkScalarBarWidget表现ScalarBar

             ////可以移动位置大小  鼠标事件
             scalarWidget = vtkScalarBarWidget.New();
             //scalarWidget.SetInteractor(iren);
             scalarWidget.GetScalarBarActor().SetTitle("Head");
             scalarWidget.GetScalarBarActor().SetLookupTable(lut);
             //scalarWidget.GetScalarBarActor().SetLookupTable(mapper.GetLookupTable());
             //scalarWidget.SetScalarBarActor(scalarBarActor);
             scalarWidget.ResizableOn();
             scalarWidget.RepositionableOn();
             scalarWidget.SetCurrentRenderer(this.ParentNode.Scene.VtkRenderer);
             scalarWidget.SetInteractor(this.ParentNode.Scene.VtkInteractor);
             scalarWidget.GetScalarBarActor().SetHeight(10);
             scalarWidget.GetScalarBarActor().SetWidth(0.2);
             scalarWidget.GetScalarBarActor().SetPosition(0.1, 10);
             scalarWidget.GetScalarBarActor().SetOrientationToVertical ();
             //scalarWidget.GetScalarBarActor().GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport();
             scalarWidget.Modified();
             scalarWidget.SetEnabled(1);
             //scalarWidget.SetResizable(1);
             //scalarWidget.SetSelectable(1);
             scalarWidget.On();
          

             #endregion

             //this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(actor);
             //this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(scalarBarActor);


         }


         public override void StopRender()
         {
             if (this.scalarWidget!=null )
             {
                 this.scalarWidget.Dispose();
             }
             if (this.lut!=null )
             {
                 this.lut.Dispose();
             }


             //if (this.FilterTransform !=null )
             //{
             //    this.FilterTransform.Dispose();
             //}



             throw new NotImplementedException();
         }



         #region 属性改变方法

         /// <summary>
         /// 重写可见性属性
         /// </summary>
         protected override void OnSetVisibility()
         {
             base.OnSetVisibility();

             if (this.ParentNode.Scene == null)
             {
                 return;

             }

             if (this.ParentNode.Scene.IsRendering)
             {
                 if (this._visiability)
                 {
                   this.  scalarWidget.On();
                     //this.actor.VisibilityOn();
                 }
                 else
                 {
                     this.scalarWidget.Off ();
                     //this.actor.VisibilityOff();
                 }

                 this.ParentNode.Scene.Update();
             }
         }


         //protected override void OnSetRepresentationType()
         //{
         //    base.OnSetRepresentationType();


         //    if (this.ParentNode.Scene == null)
         //    {
         //        return;

         //    }

         //    if (this.ParentNode.Scene.IsRendering)
         //    {
         //        if (this._repType == REPRESENTATIONTYPE.POINT)
         //        {
         //            //this.actor.GetProperty().SetRepresentationToPoints();
         //        }
         //        else if (this._repType == REPRESENTATIONTYPE.SURFACE)
         //        {
         //            //this.actor.GetProperty().SetRepresentationToSurface();
         //        }
         //        else
         //        {
         //            //this.actor.GetProperty().SetRepresentationToWireframe();
         //        }


         //        this.ParentNode.Scene.Update();
         //    }

         //}





         #endregion


     }




}
